package cc.customer.web.Market;

import cc.customer.ServicesUtils.NoticeUtils;
import cc.customer.authorization.annotation.CurrentUser;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.DateUtil;
import cc.customer.myCustomer.damain.MyCustomerGroup;
import cc.customer.myCustomer.services.MyCustomerGroupService;
import cc.customer.rabbit.domain.CconvNotice;
import cc.customer.saleHelp.domain.CsalePlan;
import cc.customer.saleHelp.services.CsaleModalServices;
import cc.customer.saleHelp.services.CsalePlanServices;
import cc.customer.sendMsg.SendMagRecevicer;
import cc.customer.system.domain.MyUser;
import cc.customer.web.base.BaseController;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @MethodName: $
 * @Description: TODO
 * @Param: $
 * @Return: $
 * @Author: cunfeng
 * @Date: 2020/6/18 9:53
 **/
@Controller
@RequestMapping("/app/sale")
public class SaleHelpController extends BaseController {

    @Autowired
    private MyCustomerGroupService mgService;

    @Autowired
    private CsaleModalServices csaleModalServices;

    @Autowired
    private CsalePlanServices csalePlanServices;
    @Autowired
    private SendMagRecevicer sendMagRecevicer;


    /**计划添加客户**/
    @RequestMapping(value="selectGroupListTree")
    @ResponseBody
    public ResponseBo selectGroupListTree( @CurrentUser MyUser user){
        String userid = user.getUserUuid();
        MyCustomerGroup group = new MyCustomerGroup();
        group.setcGroupUserid(userid);
        JSONArray list = mgService.selectAllGroupToArray(group);
        return ResponseBo.ok(list);
    }


    /**保存销售计划**/
    @RequestMapping(value = "/saveScalePlan")
    @ResponseBody
    public ResponseBo saveScalePlan(CsalePlan csalePlan,@CurrentUser MyUser user) throws Exception {
        /**验证时间**/
        String time= csalePlan.getcPlanTime();
        Date dateNo =  new Date();
        Date date  = DateUtil.convert2Date(time,"yyyy-MM-dd HH:mm");
        Long inum  = DateUtil.diffTimes(dateNo,date);
        if (inum <= 0){
            return ResponseBo.error("执行时间不能小于当前时间");
        }

        String userid = user.getUserUuid();
        csalePlan.setcPlanUserid(userid);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                String type = csalePlan.getcPlanType();
                CconvNotice notice = NoticeUtils.planSaleNotice(type,user);
                sendMagRecevicer.processUser(notice);
            }
        }, date);
        try {
            return csalePlanServices.saveCsalePlan(csalePlan);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error();
    }


    /**查询个人的工作计划**/
    @RequestMapping(value = "/selectScalePlan")
    @ResponseBody
    public Map<String,Object> selectScalePlan( @CurrentUser MyUser user){
        Map<String,Object> map = new HashMap<String,Object>();
        CsalePlan csalePlan = new CsalePlan();
        try {
            ResponseBo responseBo = new ResponseBo();
            String userid = user.getUserUuid();
            csalePlan.setcPlanUserid(userid);
            csalePlan.setcPlanSelectCondition("0");


            responseBo = csalePlanServices.selectAllMyCsalePlan(csalePlan);

            map.put("noData",responseBo.get("msg"));
            map.put("msg","查阅成功");
            csalePlan.setcPlanSelectCondition("1");
            responseBo = csalePlanServices.selectAllMyCsalePlan(csalePlan);
            map.put("yesData",responseBo.get("msg"));
            csalePlan.setcPlanSelectCondition("2");
            responseBo = csalePlanServices.selectAllMyCsalePlan(csalePlan);
            map.put("jData",responseBo.get("msg"));
            csalePlan.setcPlanSelectCondition("3");
            responseBo = csalePlanServices.selectAllMyCsalePlan(csalePlan);
            map.put("sunData",responseBo.get("msg"));
            map.put("msg","查阅成功");
            map.put("code",0);
            return  map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",1);
            map.put("msg","查阅失败");
            return  map;
        }


    }

    /**编辑删除**/
    @RequestMapping(value = "/editScalePlanfordelete")
    @ResponseBody
    public ResponseBo editScalePlanfordelete(CsalePlan csalePlan){
        return csalePlanServices.updateMyCsaleDetail(csalePlan);
    }

    /**编辑保存**/
    @RequestMapping(value = "/editScalePlanforupdate")
    @ResponseBody
    public ResponseBo editScalePlanforupdate(CsalePlan csalePlan){
        /**验证时间**/
        String time= csalePlan.getcPlanTime();
        Date dateNo =  new Date();
        Date date  = DateUtil.convert2Date(time,"yyyy-MM-dd HH:mm");
        Long inum  = DateUtil.diffTimes(dateNo,date);
        if (inum <= 0){
            return ResponseBo.error("执行时间不能小于当前时间");
        }
        return csalePlanServices.updateMyCsaleDetail(csalePlan);
    }


    /**查询销售计划详情**/
    @RequestMapping(value = "/selectSacleDetail")
    @ResponseBody
    public ResponseBo selectSacleDetail(CsalePlan csalePlan){
        return csalePlanServices.selectAppAMyCsalePlanDetail(csalePlan);
    }


}
